package com.cskaoyan.gateway.controller.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;

import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.IKaptchaService;
import com.mall.user.UserLoginService;

import com.mall.user.annotation.Anoymous;
import com.mall.user.bo.UserLoginBO;
import com.mall.user.constants.SysRetCodeConstants;

import com.mall.user.dto.*;
import com.mall.user.intercepter.TokenIntercepter;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("user")
public class UserLoginController {


    @Reference(timeout = 3000,retries = 1, check = false)
    private UserLoginService userLoginService;

    @Reference(timeout = 3000,retries = 1, check = false)
    private IKaptchaService iKaptchaService;

    @GetMapping("/login")
    public ResponseData getLogin(HttpServletRequest request) {
        try {
            String token = CookieUtil.getCookieValue(request, "access_token");
            if (StringUtils.isEmpty(token)) {
                return new ResponseUtil<>().setErrorMsg("token已失效");
            }
            //解密验证
            VerifyTokenResponse tokenResponse = userLoginService.verifyToken(token);
            if (!SysRetCodeConstants.SUCCESS.getCode().equals(tokenResponse.getCode())) {
                return new ResponseUtil<>().setErrorMsg("解密失败");
            }
            return new ResponseUtil<>().setData(tokenResponse);

        } catch (Exception e) {
            return new ResponseUtil().setData("验证失败");
        }
    }


    @PostMapping("/login")
    @Anoymous
    public ResponseData postLogin(@RequestBody UserLoginBO bo,HttpServletRequest request, HttpServletResponse response) {
        //验证验证码是否正确
        String kaptcha_uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        String captcha = bo.getCaptcha();
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        kaptchaCodeRequest.setUuid(kaptcha_uuid);
        kaptchaCodeRequest.setCode(captcha);//验证码
        KaptchaCodeResponse kaptchaCodeResponse = iKaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        if(!SysRetCodeConstants.SUCCESS.getCode().equals(kaptchaCodeResponse.getCode())){
            return new ResponseUtil<>().setErrorMsg(kaptchaCodeResponse.getMsg());
        }
        //验证用户信息是否正确
        UserLoginResponse userLoginResponse = userLoginService.login(bo);
        if (userLoginResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            //用户登陆成功
            String token = userLoginResponse.getToken();
            Cookie cookie = CookieUtil.genCookie("access_token", token, "/", 3600*24);
            response.addCookie(cookie);
            return new ResponseUtil<>().setData(userLoginResponse);
        }
        return new ResponseUtil<>().setErrorMsg(userLoginResponse.getMsg());
    }
    @GetMapping("loginOut")
    public ResponseData loginOut(HttpServletRequest request,HttpServletResponse response) {
        Cookie cookie = CookieUtil.genCookie("access_token",  null, "/", 0);
        response.addCookie(cookie);
        return new ResponseUtil().setData(null);
    }
}
